<template>
  <FormItemRest>
    <BoTable
      :columns="columns"
      :request="() => getDeviceListByPowerSupply(powerId)"
      ref="tableRef"
      row-key="id"
      height="auto"
      :show-index="true"
      hideSearch
    ></BoTable>
  </FormItemRest>
</template>

<script setup lang="tsx">
import { computed, ref } from 'vue';
import { getDeviceManufacturerSelect, getDeviceModelSelect } from '@/api';
import { useDicts } from '@boeyo/portal';
import { listToEnum, listToOption } from '@boeyo/shared';
import { BoTable, BoTableProps } from '@boeyo/ui';
import { FormItemRest } from 'ant-design-vue';
import { getDeviceListByPowerSupply } from '../api';
import { deviceEnum } from '../constants';

const tableRef = ref();

// 获取表单下拉框数据
const mnufacturerOptions = ref<any[]>([]);
const manufacturerEnum = ref<Record<string, any>>({});
const modelOptions = ref<any[]>([]);
const modelEnum = ref<Record<string, any>>({});
const init = () => {
  getDeviceManufacturerSelect('').then(({ data }) => {
    mnufacturerOptions.value = listToOption(data, 'name', 'facId');
    manufacturerEnum.value = listToEnum(data, 'name', 'facId');
  });
  getDeviceModelSelect({}).then(({ data }) => {
    modelOptions.value = listToOption(data, 'model', 'modelId');
    modelEnum.value = listToEnum(data, 'model', 'modelId');
  });
};
init();

const dictArr = ['device_type'];
const { deviceTypeEnum } = useDicts(dictArr);

defineProps({
  powerId: {
    type: Number,
    default: 0,
  },
});

const columns = computed<BoTableProps['columns']>(() => [
  {
    title: '设备编号',
    dataIndex: 'stationId',
    hideInSearch: true,
  },
  {
    title: '设备类型',
    dataIndex: 'deviceType',
    valueEnum: deviceTypeEnum.value,
    hideInSearch: true,
  },
  {
    title: '设备型号',
    dataIndex: 'modelId',
    valueEnum: modelEnum.value,
    hideInSearch: true,
  },
  {
    title: '设备厂商',
    dataIndex: 'facId',
    valueEnum: manufacturerEnum.value,
    hideInSearch: true,
  },
]);
</script>

<style lang="scss" scoped></style>
